package com.ljb.webviewdemo.crosswalk;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.ljb.webviewdemo.R;

import org.xwalk.core.XWalkActivity;
import org.xwalk.core.XWalkDownloadListener;
import org.xwalk.core.XWalkNavigationHistory;
import org.xwalk.core.XWalkPreferences;
import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkSettings;
import org.xwalk.core.XWalkUIClient;
import org.xwalk.core.XWalkView;

/**
 * @author ljb
 * @des
 * @date 2019/1/23
 */
public class CrossWalkActivity extends XWalkActivity {

    private XWalkView xWalkView;
    private ProgressBar mPageLoadingProgressBar;

    public static void actionStart(Context context, String url) {
        Intent intent = new Intent(context, CrossWalkActivity.class);
        intent.putExtra("URL", url);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crosswalk);
        initProgressBar();
    }

    private void initProgressBar() {
        mPageLoadingProgressBar = findViewById(R.id.progressBar1);
        mPageLoadingProgressBar.setMax(100);
        mPageLoadingProgressBar.setProgressDrawable(this.getResources().getDrawable(R.drawable.color_progressbar));
    }

    @Override
    protected void onXWalkReady() {
        xWalkView = findViewById(R.id.xWalkView);
        XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, true);
        XWalkSettings settings = xWalkView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setBuiltInZoomControls(true);
        settings.supportMultipleWindows();
        settings.setSupportZoom(true);
        settings.getAllowFileAccess();
        settings.setDomStorageEnabled(true);
        settings.getAllowContentAccess();
        settings.setLayoutAlgorithm(XWalkSettings.LayoutAlgorithm.SINGLE_COLUMN);


        xWalkView.requestFocus();
        xWalkView.setResourceClient(new XWalkResourceClient(xWalkView) {
            @Override
            public boolean shouldOverrideUrlLoading(XWalkView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onProgressChanged(XWalkView view, int progressInPercent) {
                if (progressInPercent == 100 && mPageLoadingProgressBar != null) {
                    mPageLoadingProgressBar.setVisibility(View.GONE);
                } else {
                    if (mPageLoadingProgressBar != null) {
                        mPageLoadingProgressBar.setVisibility(View.VISIBLE);
                        mPageLoadingProgressBar.setProgress(progressInPercent);
                    }
                }
            }
        });
        xWalkView.setUIClient(new XWalkUIClient(xWalkView));
        xWalkView.setDownloadListener(new XWalkDownloadListener(this) {
            @Override
            public void onDownloadStart(String s, String s1, String s2, String s3, long l) {
                new AlertDialog.Builder(CrossWalkActivity.this)
                        .setTitle("是否允许下载？")
                        .setPositiveButton("下载", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(CrossWalkActivity.this, "开始下载...", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(CrossWalkActivity.this, "下载取消...", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setOnCancelListener(new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialog) {
                                // TODO Auto-generated method stub
                                Toast.makeText(CrossWalkActivity.this, "下载取消...", Toast.LENGTH_SHORT).show();
                            }
                        }).show();
            }
        });


        xWalkView.loadUrl(getIntent().getStringExtra("URL"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (xWalkView != null) {
            xWalkView.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (xWalkView != null) {
            xWalkView.onNewIntent(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (xWalkView != null) {
            xWalkView.resumeTimers();
            xWalkView.onShow();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (xWalkView != null) {
            xWalkView.pauseTimers();
            xWalkView.onHide();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (xWalkView != null && xWalkView.getNavigationHistory().canGoBack()) {
                xWalkView.getNavigationHistory().navigate(XWalkNavigationHistory.Direction.BACKWARD, 1);
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
